/*
 * CEnergyRegen.java
 * 
 * Copyright (c) 2009 Giles Millward (Manigra). All rights reserved.
 * 
 * This file is part of Expression program is undefined on line 8, column 40 in Templates/Licenses/license-gplv3.txt..
 * 
 * Expression program is undefined on line 10, column 19 in Templates/Licenses/license-gplv3.txt. is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Expression program is undefined on line 15, column 19 in Templates/Licenses/license-gplv3.txt. is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Expression program is undefined on line 21, column 30 in Templates/Licenses/license-gplv3.txt..  If not, see <http://www.gnu.org/licenses/>.
 */

package dpssystem;

import dpsstats.CDamageStats;
import dpsstats.CFinisherStats;
import dpssystem.CAttackChance.HitType;
import java.util.ArrayList;

/**
 *
 * @author Mani
 */
public class CRupture extends CAbility {

    CDamageStats m_damageStats;
    CDamageStats m_tickDamageStats;
    CFinisherStats m_finisherStats;
    boolean m_allowCrits = false;
    double m_dmg[] = {0, 145, 163, 181, 199, 217};
    double m_atkMod[] = {0.0, 0.015, 0.024, 0.03, 0.03428571, 0.0375};
    double m_extendTime = 0.0;
    double m_tickTime = 2.0;
    int m_comboPointsUsed = 0; // Tracker of the combo points used
    int m_backStabTicks = 0; // Backstab can extend rupture time up to 6 seconds
    double m_serratedBlades = 0.0;

    CRupture(CSim p_sim)
    {
        super(p_sim, true);
        m_energyRequired = 25;
        m_damageStats = m_sim.m_stats.m_ruptureStats;
        m_tickDamageStats = m_sim.m_stats.m_ruptureDOTStats;
        m_finisherStats = m_sim.m_stats.m_finisherStatsList.get(3);

        // Tier 8 4piece bonus allows rupture to crit
        if (m_sim.m_dpsInterface.m_bonusSet.isTier8_4pc())
        {
            m_allowCrits = true;
        }

        if (m_sim.m_dpsInterface.m_glyphs.m_rupture)
        {
            m_extendTime = 4;
        }
        
        m_serratedBlades = m_sim.m_dpsInterface.m_talents.getSerrateBladesDouble() * 10.0;
    }

    public boolean canExecute()
    {
        if (m_sim.m_player.m_energyRegen.m_currentEnergy < m_energyRequired)
        {
            // Not enough energy
            return false;
        }

        if (m_sim.m_currentRunTime < m_endOfTimer)
        {
            // Rupture still running
            return false;
        }

        if (m_sim.m_target.m_comboPoints.m_comboPoints == 0)
        {
            // Need combo points
            return false;
        }

        if (m_sim.m_currentRunTime < m_sim.m_gcd.m_endOfGCD)
        {
            // Still on GCD
            return false;
        }

        return true;
    }

    public void execute()
    {
        m_sim.m_gcd.startGCD();
        m_sim.m_player.m_energyRegen.useAbilityEnergy(m_energyRequired);
        // Special attack type for main hand
        HitType l_hitType = m_sim.m_attackChance.getFinisherAttackHit(m_sim.m_baseStats.getHitChance(), m_sim.m_baseStats.getExpertiseDodge());
        if (l_hitType != HitType.NORMAL)
        {
            // We missed
            m_finisherStats.addFinisher(m_sim.m_target.m_comboPoints.m_comboPoints);
            m_sim.m_target.m_comboPoints.m_comboPoints = 0;
            m_damageStats.addStats(l_hitType, 0.0);
            return;
        }

        // Hit the target so start up rupture
        // Remove any ticks remaining in the event list
        m_sim.removeEventClass(CRuptureTickEvent.class);

        // Fire up an event to start ticking
        m_sim.m_eventList.add(new CRuptureTickEvent(m_sim.m_currentRunTime + m_tickTime));

        // Track the number of combo points used in the finisher
        m_comboPointsUsed = m_sim.m_target.m_comboPoints.m_comboPoints;
        m_endOfTimer = m_sim.m_currentRunTime +
                        6 +
                        (m_comboPointsUsed * 2) +
                        m_extendTime;

        m_damageStats.addStats(l_hitType, 0.0);
        m_finisherStats.addFinisher(m_sim.m_target.m_comboPoints.m_comboPoints);
        m_sim.m_target.m_comboPoints.m_comboPoints = 0;
        m_backStabTicks = 0;

        // Proc ruthlessness for combo point renewal
        m_sim.m_target.m_comboPoints.ruthlessness();
        // Proc relentless strikes for energy
        m_sim.m_player.m_energyRegen.relentlessStrikes();
    }

    public void backStab()
    {
        if (m_sim.m_currentRunTime < this.m_endOfTimer)
        {
            if (this.m_backStabTicks < 3)
            {
                // Extend rupture time if backstab used
                this.m_endOfTimer += 2.0;
                this.m_backStabTicks += 1;
            }
        }
    }

    // Rupture Ticks
    public class CRuptureTickEvent extends CEvent {

        CRuptureTickEvent(double p_eventTime)
        {
            super(p_eventTime);
        }

        public ArrayList<CEvent>runEvent(CSim p_sim)
        {
            // TODO Dirty Deeds
            if (p_sim.m_currentRunTime <= (m_endOfTimer + 0.02))
            {
                HitType l_hitType = HitType.NORMAL;
                double l_damage = m_dmg[m_comboPointsUsed] + m_atkMod[m_comboPointsUsed] * m_sim.m_baseStats.getAttackPower();
                // TODO damage modifiers
                if (m_allowCrits)
                {
                    if (m_sim.m_rng.nextDouble() <= m_sim.m_baseStats.getCritChance())
                    {
                        l_hitType = HitType.CRIT;
                        // TODO Crit bonus
                        l_damage *= 2.0;
                    }
                }

                double l_resisted = (l_damage * 0.06);
                l_damage -= l_resisted;
                m_tickDamageStats.addStats(l_hitType, l_damage);

                // Add another tick on
                ArrayList<CEvent> l_newEventList = new ArrayList<CEvent>();
                l_newEventList.add(new CRuptureTickEvent(p_sim.m_currentRunTime + 2.0));
                return l_newEventList;
            }

            // No new tick
            return null;
        }
    }
}
